package com.vue.power.backend.sys.controller;

import com.vue.power.backend.common.base.constant.SystemStaticConst;
import com.vue.power.backend.common.util.token.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/file")
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Value("${file.upload.path}")
    private String uploadPath;

    /**
     * 功能描述：实现文件上传
     * @param file
     * @return
     */
    @PostMapping(value = "uploadFile")
    public Map<String,Object> uploadFile(@RequestParam("file") MultipartFile file,
                                         @RequestParam("idImageType") String idImageType) {
        Map<String,Object> result = new HashMap<>();
        if (file.isEmpty()) {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"上传文件不能为空！");
            return result;
        }
        if(idImageType==null||idImageType.equals("")){
            idImageType = "tmp";
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        logger.info("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        logger.info("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String filePath = uploadPath;
        if(filePath==null||filePath.equalsIgnoreCase("")){
            filePath = "E://test//";
        }
        filePath = filePath + idImageType + "//";
        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = TokenUtil.getUuid() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"上传文件成功");
            Map<String,Object> entity = new HashMap<>();
            entity.put("name",idImageType+"/"+fileName);
            entity.put("url",idImageType+"/"+fileName);
            result.put(SystemStaticConst.ENTITY,entity);
            return result;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"上传文件失败！失败原因："+e.getMessage());
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
        result.put(SystemStaticConst.MSG,"上传文件失败！");
        return result;
    }


}
